<?php

/**
 * Class used to display content pages on template
 * @author shenlon 
 */
class View {

    /**
     * Variables passed to template file
     * @var Array
     */
    protected $variables = array(
        'viewFolder' => 'View/',
        'format' => 'phtml',
        'scripts' => array(),
    );

    /**
     * Path to template file
     * @var String
     */
    protected $templateFile;

    /**
     * Message used for error occuring when file can't be read
     * @var String 
     */

    const FILE_NOT_FOUND = "Can't read file %s";

    /**
     * Default constructor
     * @param string $templateFile
     * @param Array $options
     * @throws Exception 
     */
    public function __construct($templateFile, $options = null) {
        if ($options != null) {
            if (!is_array($options))
                throw new Exception("Options has to be an array");
            foreach ($options as $option => $value)
                $this->$option = $value;
        }
        if ($templateFile == null)
            throw new Exception("Template file is not specified");
        $this->templateFile = $templateFile;
    }

    /**
     * Returns path to script file
     * @param string $file
     * @return string
     */
    private function getScriptFile($file) {
        return $this->viewFolder . $file . '.' . $this->format;
    }

    /**
     * Sets status of an action as well as whether it was successful
     * @param string $text
     * @param boolean $success 
     */
    public function setStatus($text, $success = true) {
        $this->variables["statusText"] = $text;
        $this->variables["statusType"] = $success;
    }

    public function __isset($name) {
        if (array_key_exists($name, $this->variables))
            return true;
        return false;
    }

    public function __set($name, $value) {
        if ($name == 'templateFile' && !is_readable($value))
            throw new Exception(sprintf(self::FILE_NOT_FOUND, $value));
        if (property_exists($this, $name))
            $this->name = $value;
        else
            $this->variables[$name] = $value;
    }

    public function __get($name) {
        if (property_exists($this, $name))
            return $this->$name;
        else if (array_key_exists($name, $this->variables))
            return $this->variables[$name];
        throw new Exception("Variable $name does not exist.");
    }

    /**
     * Renders and echoes (if set) template file with content from content file
     * @param string $contentFile
     * @param boolean $verbose
     * @return string
     * @throws Exception 
     */
    public function render($contentFile = null, $verbose = false) {
        if ($contentFile != null) {
            $contentFile = $this->getScriptFile($contentFile);
            if (!is_readable($contentFile))
                throw new Exception(sprintf(self::FILE_NOT_FOUND, $contentFile));
            $this->content = $this->process($contentFile);
        }
        $this->content = $this->process($this->templateFile);
        if ($verbose)
            echo $this->content;
        return $this->content;
    }

    /**
     * Processes content file
     * @return string
     */
    public function process() {
        extract($this->variables, EXTR_OVERWRITE);
        ob_start();
        include func_get_arg(0);
        return ob_get_clean();
    }

}

?>